package logic

import (
	"context"
	"database/sql"
	"errors"
	"fmt"
	"zero-demo-rpc/mall/MakeProduct/api/internal/svc"
	"zero-demo-rpc/mall/MakeProduct/api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type ProductInfoLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

type ProductInfo struct {
	nftname         string `db:"nftname"`
	description     string `db:"discription"`
	amount          int    `db:"amount"`
	contractaddress string `db:"contractaddress"`
	contractinfo    string `db:"identification"`
	time            string `db:"time"`
	state           int    `db:"state"`
	hash            string `db:"hash"`
	imgurl          string `db:"imgurl"`
}

func NewProductInfoLogic(ctx context.Context, svcCtx *svc.ServiceContext) *ProductInfoLogic {
	return &ProductInfoLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

func (l *ProductInfoLogic) ProductInfo(req *types.InfoReq) (resp *types.InfoReply, err error) {
	// todo: add your logic here and delete this line

	print("接收列表请求：", req.Id)

	db, err := sql.Open("mysql", "root:huohuo@tcp(127.0.0.1:3306)/test_2?charset=utf8")

	//root为数据库用户名，后面为密码，tcp代表tcp协议，test处填写自己的数据库名称
	rows, err := db.Query("SELECT product_name,product_description,product_state,product_count,product_url,contract_address,contract_info FROM product_list where product_id=?", req.Id)
	var s ProductInfo
	if err != nil {
		return nil, errors.New("")
	} else {
		for rows.Next() {
			err := rows.Scan(&s.nftname, &s.description, &s.state, &s.amount, &s.imgurl, &s.contractaddress, &s.contractinfo)
			//err := rows.Scan(&s.name, &s.address)
			fmt.Println(err)
		}
	}

	rows, err = db.Query("SELECT trade_id,trade_time FROM product_info where product_id=?", req.Id)
	if err != nil {
		return nil, errors.New("")
	} else {

		for rows.Next() {
			err := rows.Scan(&s.hash, &s.time)
			//err := rows.Scan(&s.name, &s.address)
			fmt.Println(err)
		}
	}

	return &types.InfoReply{
		NftName:         s.nftname,
		Description:     s.description,
		Amount:          s.amount,
		ContractAddress: s.contractaddress,
		Identification:  s.contractinfo,
		Time:            s.time,
		State:           s.state,
		Hash:            s.hash,
		ImgURL:          "http://192.168.0.50:12302/imgs/" + s.imgurl,
	}, err
	return nil, err
	//return &data, nil
}
